﻿using System;
using fastCSharp;

namespace diantou.web.contest
{
    /// <summary>
    /// 比赛
    /// </summary>
    /// <typeparam name="viewType">页面类型</typeparam>
    abstract class view<viewType> : web.view<viewType>
        where viewType : view<viewType> 
    {
        /// <summary>
        /// 比赛
        /// </summary>
        [fastCSharp.emit.webView.clearMember]
        protected diantou.dataAccess.contest contest;
        /// <summary>
        /// 用户比赛
        /// </summary>
        protected userContest userContest
        {
            get { return new userContest { Contest = contest, User = currentUser }; }
        }
        /// <summary>
        /// 问题列表
        /// </summary>
        protected onlineJudge.userProblem[] problems
        {
            get
            {
                if (date.NowSecond >= contest.StartTime && contest.Type != dataModel.contest.type.External)
                {
                    diantou.dataAccess.problem[] values;
                    int index = 0;
                    if (diantou.dataAccess.clientCache.ContestProblem.IsData)
                    {
                        values = diantou.dataAccess.clientCache.Problem.Get(diantou.dataAccess.tcpCall.contestProblem.getProblemIds(contest.Id, currentUserId));
                        foreach (diantou.dataAccess.problem problem in values)
                        {
                            int point = diantou.dataAccess.clientCache.ContestProblem[new dataModel.contestProblem.primaryKey { ContestId = contest.Id, ProblemId = problem.Id }];
                            if (point >= 0 && point != problem.BasePoint) (values[index] = problem.Clone()).BasePoint = point;
                            ++index;
                        }
                        index = 0;
                    }
                    else
                    {
                        int[] points;
                        values = diantou.dataAccess.tcpCall.contestProblem.getProblems(contest.Id, currentUserId, out points);
                        diantou.dataAccess.contestProblem.SetPoint(values, points);
                    }
                    return values.getArray(value => new onlineJudge.userProblem { ContestLetter = (char)('A' + index++), Problem = value, User = currentUser });
                }
                return nullValue<onlineJudge.userProblem>.Array;
            }
        }
        /// <summary>
        /// 相关收藏top5
        /// </summary>
        protected userContest.relateFavorite relateFavorite
        {
            get
            {
                return new userContest.relateFavorite { ViewOnly = requestHeader.IsReView, Contest = new userContest { Contest = contest, User = currentUser } };
            }
        }
        /// <summary>
        /// 用户图片集合
        /// </summary>
        protected focusImage joinImage
        {
            get
            {
                if (contest.Type == dataModel.contest.type.External || requestHeader.IsReView) return new focusImage { ViewOnly = true };
                int count;
                diantou.dataAccess.user[] users;
                if (diantou.dataAccess.clientCache.ContestRegister.IsData)
                {
                    users = diantou.dataAccess.clientCache.User.Get(contestRegister.Default.GetUserIds(contest.Id).toSubArray().GetPageDesc(4 * 10, 1));
                }
                else users = diantou.dataAccess.clientCache.User.Get(diantou.dataAccess.tcpCall.focusContest.getUserIds(contest.Id, diantou.dataModel.focus.type.Register, 4 * 10, 1, out count));
                return new focusImage(users, 10);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="problemId"></param>
        /// <returns></returns>
        protected virtual bool loadView(int contestId, string randomCode)
        {
            if (load(contestId, randomCode)) return true;
            AjaxResponse(webConfig.NotFound404);
            return false;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="problemId"></param>
        /// <returns></returns>
        protected bool load(int contestId, string randomCode)
        {
            if (contestId == 0)
            {
                checkSearchEngine(diantou.dataModel.path.pub.Default.ContestListPath, webConfig.NotFound404);
                return false;
            }
            contest = diantou.dataAccess.clientCache.Contest[contestId];
            if (contest != null)
            {
                if (currentUser != null && currentUser.Permission == dataModel.user.permission.Admin) return true;
                if (contest.IsShow && (contest.RandomCode == 0 || contest.RandomCode == fastCSharp.number.parseHex16NoCheck(randomCode))) return true;
            }
            AjaxResponse(webConfig.NotFound404);
            return false;
        }
    }
}
